mirror of
https://github.com/kennethnym/aris.git
synced 2026-03-20 09:01:19 +00:00
Compare commits
10 Commits
refactor/r
...
feat/feed-
| Author | SHA1 | Date | |
|---|---|---|---|
|
08dd437952
|
|||
| 2fc20759dd | |||
| 769e2d4eb0 | |||
|
5e9094710d
|
|||
|
5556f3fbf9
|
|||
|
0176979925
|
|||
|
971aba0932
|
|||
|
68e319e4b8
|
|||
| c042af88f3 | |||
|
0608f2ac61
|
@@ -46,7 +46,97 @@
|
||||
}
|
||||
}
|
||||
],
|
||||
"expo-font"
|
||||
[
|
||||
"expo-font",
|
||||
{
|
||||
"android": {
|
||||
"fonts": [
|
||||
{
|
||||
"fontFamily": "Inter",
|
||||
"fontDefinitions": [
|
||||
{ "path": "./assets/fonts/Inter_100Thin.ttf", "weight": 100 },
|
||||
{ "path": "./assets/fonts/Inter_100Thin_Italic.ttf", "weight": 100, "style": "italic" },
|
||||
{ "path": "./assets/fonts/Inter_200ExtraLight.ttf", "weight": 200 },
|
||||
{ "path": "./assets/fonts/Inter_200ExtraLight_Italic.ttf", "weight": 200, "style": "italic" },
|
||||
{ "path": "./assets/fonts/Inter_300Light.ttf", "weight": 300 },
|
||||
{ "path": "./assets/fonts/Inter_300Light_Italic.ttf", "weight": 300, "style": "italic" },
|
||||
{ "path": "./assets/fonts/Inter_400Regular.ttf", "weight": 400 },
|
||||
{ "path": "./assets/fonts/Inter_400Regular_Italic.ttf", "weight": 400, "style": "italic" },
|
||||
{ "path": "./assets/fonts/Inter_500Medium.ttf", "weight": 500 },
|
||||
{ "path": "./assets/fonts/Inter_500Medium_Italic.ttf", "weight": 500, "style": "italic" },
|
||||
{ "path": "./assets/fonts/Inter_600SemiBold.ttf", "weight": 600 },
|
||||
{ "path": "./assets/fonts/Inter_600SemiBold_Italic.ttf", "weight": 600, "style": "italic" },
|
||||
{ "path": "./assets/fonts/Inter_700Bold.ttf", "weight": 700 },
|
||||
{ "path": "./assets/fonts/Inter_700Bold_Italic.ttf", "weight": 700, "style": "italic" },
|
||||
{ "path": "./assets/fonts/Inter_800ExtraBold.ttf", "weight": 800 },
|
||||
{ "path": "./assets/fonts/Inter_800ExtraBold_Italic.ttf", "weight": 800, "style": "italic" },
|
||||
{ "path": "./assets/fonts/Inter_900Black.ttf", "weight": 900 },
|
||||
{ "path": "./assets/fonts/Inter_900Black_Italic.ttf", "weight": 900, "style": "italic" }
|
||||
]
|
||||
},
|
||||
{
|
||||
"fontFamily": "Source Serif 4",
|
||||
"fontDefinitions": [
|
||||
{ "path": "./assets/fonts/SourceSerif4_200ExtraLight.ttf", "weight": 200 },
|
||||
{ "path": "./assets/fonts/SourceSerif4_200ExtraLight_Italic.ttf", "weight": 200, "style": "italic" },
|
||||
{ "path": "./assets/fonts/SourceSerif4_300Light.ttf", "weight": 300 },
|
||||
{ "path": "./assets/fonts/SourceSerif4_300Light_Italic.ttf", "weight": 300, "style": "italic" },
|
||||
{ "path": "./assets/fonts/SourceSerif4_400Regular.ttf", "weight": 400 },
|
||||
{ "path": "./assets/fonts/SourceSerif4_400Regular_Italic.ttf", "weight": 400, "style": "italic" },
|
||||
{ "path": "./assets/fonts/SourceSerif4_500Medium.ttf", "weight": 500 },
|
||||
{ "path": "./assets/fonts/SourceSerif4_500Medium_Italic.ttf", "weight": 500, "style": "italic" },
|
||||
{ "path": "./assets/fonts/SourceSerif4_600SemiBold.ttf", "weight": 600 },
|
||||
{ "path": "./assets/fonts/SourceSerif4_600SemiBold_Italic.ttf", "weight": 600, "style": "italic" },
|
||||
{ "path": "./assets/fonts/SourceSerif4_700Bold.ttf", "weight": 700 },
|
||||
{ "path": "./assets/fonts/SourceSerif4_700Bold_Italic.ttf", "weight": 700, "style": "italic" },
|
||||
{ "path": "./assets/fonts/SourceSerif4_800ExtraBold.ttf", "weight": 800 },
|
||||
{ "path": "./assets/fonts/SourceSerif4_800ExtraBold_Italic.ttf", "weight": 800, "style": "italic" },
|
||||
{ "path": "./assets/fonts/SourceSerif4_900Black.ttf", "weight": 900 },
|
||||
{ "path": "./assets/fonts/SourceSerif4_900Black_Italic.ttf", "weight": 900, "style": "italic" }
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"ios": {
|
||||
"fonts": [
|
||||
"./assets/fonts/Inter_100Thin.ttf",
|
||||
"./assets/fonts/Inter_100Thin_Italic.ttf",
|
||||
"./assets/fonts/Inter_200ExtraLight.ttf",
|
||||
"./assets/fonts/Inter_200ExtraLight_Italic.ttf",
|
||||
"./assets/fonts/Inter_300Light.ttf",
|
||||
"./assets/fonts/Inter_300Light_Italic.ttf",
|
||||
"./assets/fonts/Inter_400Regular.ttf",
|
||||
"./assets/fonts/Inter_400Regular_Italic.ttf",
|
||||
"./assets/fonts/Inter_500Medium.ttf",
|
||||
"./assets/fonts/Inter_500Medium_Italic.ttf",
|
||||
"./assets/fonts/Inter_600SemiBold.ttf",
|
||||
"./assets/fonts/Inter_600SemiBold_Italic.ttf",
|
||||
"./assets/fonts/Inter_700Bold.ttf",
|
||||
"./assets/fonts/Inter_700Bold_Italic.ttf",
|
||||
"./assets/fonts/Inter_800ExtraBold.ttf",
|
||||
"./assets/fonts/Inter_800ExtraBold_Italic.ttf",
|
||||
"./assets/fonts/Inter_900Black.ttf",
|
||||
"./assets/fonts/Inter_900Black_Italic.ttf",
|
||||
"./assets/fonts/SourceSerif4_200ExtraLight.ttf",
|
||||
"./assets/fonts/SourceSerif4_200ExtraLight_Italic.ttf",
|
||||
"./assets/fonts/SourceSerif4_300Light.ttf",
|
||||
"./assets/fonts/SourceSerif4_300Light_Italic.ttf",
|
||||
"./assets/fonts/SourceSerif4_400Regular.ttf",
|
||||
"./assets/fonts/SourceSerif4_400Regular_Italic.ttf",
|
||||
"./assets/fonts/SourceSerif4_500Medium.ttf",
|
||||
"./assets/fonts/SourceSerif4_500Medium_Italic.ttf",
|
||||
"./assets/fonts/SourceSerif4_600SemiBold.ttf",
|
||||
"./assets/fonts/SourceSerif4_600SemiBold_Italic.ttf",
|
||||
"./assets/fonts/SourceSerif4_700Bold.ttf",
|
||||
"./assets/fonts/SourceSerif4_700Bold_Italic.ttf",
|
||||
"./assets/fonts/SourceSerif4_800ExtraBold.ttf",
|
||||
"./assets/fonts/SourceSerif4_800ExtraBold_Italic.ttf",
|
||||
"./assets/fonts/SourceSerif4_900Black.ttf",
|
||||
"./assets/fonts/SourceSerif4_900Black_Italic.ttf"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
],
|
||||
"experiments": {
|
||||
"typedRoutes": true,
|
||||
|
||||
BIN
apps/aris-client/assets/fonts/Inter_100Thin.ttf
Normal file
BIN
apps/aris-client/assets/fonts/Inter_100Thin.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/Inter_100Thin_Italic.ttf
Normal file
BIN
apps/aris-client/assets/fonts/Inter_100Thin_Italic.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/Inter_200ExtraLight.ttf
Normal file
BIN
apps/aris-client/assets/fonts/Inter_200ExtraLight.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/Inter_200ExtraLight_Italic.ttf
Normal file
BIN
apps/aris-client/assets/fonts/Inter_200ExtraLight_Italic.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/Inter_300Light.ttf
Normal file
BIN
apps/aris-client/assets/fonts/Inter_300Light.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/Inter_300Light_Italic.ttf
Normal file
BIN
apps/aris-client/assets/fonts/Inter_300Light_Italic.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/Inter_400Regular.ttf
Normal file
BIN
apps/aris-client/assets/fonts/Inter_400Regular.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/Inter_400Regular_Italic.ttf
Normal file
BIN
apps/aris-client/assets/fonts/Inter_400Regular_Italic.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/Inter_500Medium.ttf
Normal file
BIN
apps/aris-client/assets/fonts/Inter_500Medium.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/Inter_500Medium_Italic.ttf
Normal file
BIN
apps/aris-client/assets/fonts/Inter_500Medium_Italic.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/Inter_600SemiBold.ttf
Normal file
BIN
apps/aris-client/assets/fonts/Inter_600SemiBold.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/Inter_600SemiBold_Italic.ttf
Normal file
BIN
apps/aris-client/assets/fonts/Inter_600SemiBold_Italic.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/Inter_700Bold.ttf
Normal file
BIN
apps/aris-client/assets/fonts/Inter_700Bold.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/Inter_700Bold_Italic.ttf
Normal file
BIN
apps/aris-client/assets/fonts/Inter_700Bold_Italic.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/Inter_800ExtraBold.ttf
Normal file
BIN
apps/aris-client/assets/fonts/Inter_800ExtraBold.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/Inter_800ExtraBold_Italic.ttf
Normal file
BIN
apps/aris-client/assets/fonts/Inter_800ExtraBold_Italic.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/Inter_900Black.ttf
Normal file
BIN
apps/aris-client/assets/fonts/Inter_900Black.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/Inter_900Black_Italic.ttf
Normal file
BIN
apps/aris-client/assets/fonts/Inter_900Black_Italic.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/SourceSerif4_200ExtraLight.ttf
Normal file
BIN
apps/aris-client/assets/fonts/SourceSerif4_200ExtraLight.ttf
Normal file
Binary file not shown.
Binary file not shown.
BIN
apps/aris-client/assets/fonts/SourceSerif4_300Light.ttf
Normal file
BIN
apps/aris-client/assets/fonts/SourceSerif4_300Light.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/SourceSerif4_300Light_Italic.ttf
Normal file
BIN
apps/aris-client/assets/fonts/SourceSerif4_300Light_Italic.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/SourceSerif4_400Regular.ttf
Normal file
BIN
apps/aris-client/assets/fonts/SourceSerif4_400Regular.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/SourceSerif4_400Regular_Italic.ttf
Normal file
BIN
apps/aris-client/assets/fonts/SourceSerif4_400Regular_Italic.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/SourceSerif4_500Medium.ttf
Normal file
BIN
apps/aris-client/assets/fonts/SourceSerif4_500Medium.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/SourceSerif4_500Medium_Italic.ttf
Normal file
BIN
apps/aris-client/assets/fonts/SourceSerif4_500Medium_Italic.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/SourceSerif4_600SemiBold.ttf
Normal file
BIN
apps/aris-client/assets/fonts/SourceSerif4_600SemiBold.ttf
Normal file
Binary file not shown.
Binary file not shown.
BIN
apps/aris-client/assets/fonts/SourceSerif4_700Bold.ttf
Normal file
BIN
apps/aris-client/assets/fonts/SourceSerif4_700Bold.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/SourceSerif4_700Bold_Italic.ttf
Normal file
BIN
apps/aris-client/assets/fonts/SourceSerif4_700Bold_Italic.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/SourceSerif4_800ExtraBold.ttf
Normal file
BIN
apps/aris-client/assets/fonts/SourceSerif4_800ExtraBold.ttf
Normal file
Binary file not shown.
Binary file not shown.
BIN
apps/aris-client/assets/fonts/SourceSerif4_900Black.ttf
Normal file
BIN
apps/aris-client/assets/fonts/SourceSerif4_900Black.ttf
Normal file
Binary file not shown.
BIN
apps/aris-client/assets/fonts/SourceSerif4_900Black_Italic.ttf
Normal file
BIN
apps/aris-client/assets/fonts/SourceSerif4_900Black_Italic.ttf
Normal file
Binary file not shown.
@@ -8,6 +8,12 @@
|
||||
"developmentClient": true,
|
||||
"distribution": "internal"
|
||||
},
|
||||
"development-simulator": {
|
||||
"extends": "development",
|
||||
"ios": {
|
||||
"simulator": "true"
|
||||
}
|
||||
},
|
||||
"preview": {
|
||||
"distribution": "internal"
|
||||
},
|
||||
|
||||
@@ -11,10 +11,12 @@
|
||||
"web": "expo start --web",
|
||||
"lint": "expo lint",
|
||||
"build:ios": "eas build --profile development --platform ios --non-interactive",
|
||||
"build:ios-simulator": "eas build --profile development-simulator --platform ios --non-interactive",
|
||||
"debugger": "bun run scripts/open-debugger.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@expo-google-fonts/inter": "^0.4.2",
|
||||
"@expo-google-fonts/source-serif-4": "^0.4.1",
|
||||
"@expo/vector-icons": "^15.0.3",
|
||||
"@react-navigation/bottom-tabs": "^7.4.0",
|
||||
"@react-navigation/elements": "^2.6.3",
|
||||
|
||||
3
bun.lock
3
bun.lock
@@ -35,6 +35,7 @@
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"@expo-google-fonts/inter": "^0.4.2",
|
||||
"@expo-google-fonts/source-serif-4": "^0.4.1",
|
||||
"@expo/vector-icons": "^15.0.3",
|
||||
"@react-navigation/bottom-tabs": "^7.4.0",
|
||||
"@react-navigation/elements": "^2.6.3",
|
||||
@@ -383,6 +384,8 @@
|
||||
|
||||
"@expo-google-fonts/inter": ["@expo-google-fonts/inter@0.4.2", "", {}, "sha512-syfiImMaDmq7cFi0of+waE2M4uSCyd16zgyWxdPOY7fN2VBmSLKEzkfbZgeOjJq61kSqPBNNtXjggiQiSD6gMQ=="],
|
||||
|
||||
"@expo-google-fonts/source-serif-4": ["@expo-google-fonts/source-serif-4@0.4.1", "", {}, "sha512-Ej4UXDjW1kwYPHG8YLq6fK1bqnJGb3K35J3S5atSL0ScKFAFLKvndxoTWeCls7mybtlS9x99hzwDeXCBkiI3rA=="],
|
||||
|
||||
"@expo/apple-utils": ["@expo/apple-utils@2.1.13", "", { "bin": { "apple-utils": "bin.js" } }, "sha512-nt3efiJhAWTHl9ikKYrHEuv3dhqCdicsHFRE9LmvtcVsPhXl9bAsm0gbACoLPr7ClP8664H/S6SdVJOD/tw0jg=="],
|
||||
|
||||
"@expo/bunyan": ["@expo/bunyan@4.0.1", "", { "dependencies": { "uuid": "^8.0.0" } }, "sha512-+Lla7nYSiHZirgK+U/uYzsLv/X+HaJienbD5AKX1UQZHYfWaP+9uuQluRB4GrEVWF0GZ7vEVp/jzaOT9k/SQlg=="],
|
||||
|
||||
@@ -638,4 +638,290 @@ describe("FeedEngine", () => {
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("lastFeed", () => {
|
||||
test("returns null before any refresh", () => {
|
||||
const engine = new FeedEngine()
|
||||
|
||||
expect(engine.lastFeed()).toBeNull()
|
||||
})
|
||||
|
||||
test("returns cached result after refresh", async () => {
|
||||
const location = createLocationSource()
|
||||
location.simulateUpdate({ lat: 51.5, lng: -0.1 })
|
||||
|
||||
const weather = createWeatherSource()
|
||||
const engine = new FeedEngine().register(location).register(weather)
|
||||
|
||||
const refreshResult = await engine.refresh()
|
||||
|
||||
const cached = engine.lastFeed()
|
||||
expect(cached).not.toBeNull()
|
||||
expect(cached!.items).toEqual(refreshResult.items)
|
||||
expect(cached!.context).toEqual(refreshResult.context)
|
||||
})
|
||||
|
||||
test("returns null after TTL expires", async () => {
|
||||
const engine = new FeedEngine({ cacheTtlMs: 50 })
|
||||
const location = createLocationSource()
|
||||
location.simulateUpdate({ lat: 51.5, lng: -0.1 })
|
||||
|
||||
engine.register(location)
|
||||
await engine.refresh()
|
||||
|
||||
expect(engine.lastFeed()).not.toBeNull()
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 60))
|
||||
|
||||
expect(engine.lastFeed()).toBeNull()
|
||||
})
|
||||
|
||||
test("defaults to 5 minute TTL", async () => {
|
||||
const engine = new FeedEngine()
|
||||
const location = createLocationSource()
|
||||
location.simulateUpdate({ lat: 51.5, lng: -0.1 })
|
||||
|
||||
engine.register(location)
|
||||
await engine.refresh()
|
||||
|
||||
// Should still be cached immediately
|
||||
expect(engine.lastFeed()).not.toBeNull()
|
||||
})
|
||||
|
||||
test("refresh always fetches from sources", async () => {
|
||||
let fetchCount = 0
|
||||
const source: FeedSource = {
|
||||
id: "counter",
|
||||
...noActions,
|
||||
async fetchContext() {
|
||||
fetchCount++
|
||||
return null
|
||||
},
|
||||
}
|
||||
|
||||
const engine = new FeedEngine().register(source)
|
||||
|
||||
await engine.refresh()
|
||||
await engine.refresh()
|
||||
await engine.refresh()
|
||||
|
||||
expect(fetchCount).toBe(3)
|
||||
})
|
||||
|
||||
test("reactive context update refreshes cache", async () => {
|
||||
const location = createLocationSource()
|
||||
const weather = createWeatherSource()
|
||||
|
||||
const engine = new FeedEngine({ cacheTtlMs: 5000 }).register(location).register(weather)
|
||||
|
||||
engine.start()
|
||||
|
||||
// Simulate location update which triggers reactive refresh
|
||||
location.simulateUpdate({ lat: 51.5, lng: -0.1 })
|
||||
|
||||
// Wait for async reactive refresh to complete
|
||||
await new Promise((resolve) => setTimeout(resolve, 50))
|
||||
|
||||
const cached = engine.lastFeed()
|
||||
expect(cached).not.toBeNull()
|
||||
expect(cached!.items.length).toBeGreaterThan(0)
|
||||
|
||||
engine.stop()
|
||||
})
|
||||
|
||||
test("reactive item update refreshes cache", async () => {
|
||||
let itemUpdateCallback: (() => void) | null = null
|
||||
|
||||
const source: FeedSource = {
|
||||
id: "reactive-items",
|
||||
...noActions,
|
||||
async fetchContext() {
|
||||
return null
|
||||
},
|
||||
async fetchItems() {
|
||||
return [
|
||||
{
|
||||
id: "item-1",
|
||||
type: "test",
|
||||
priority: 0.5,
|
||||
timestamp: new Date(),
|
||||
data: {},
|
||||
},
|
||||
]
|
||||
},
|
||||
onItemsUpdate(callback) {
|
||||
itemUpdateCallback = callback
|
||||
return () => {
|
||||
itemUpdateCallback = null
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
const engine = new FeedEngine().register(source)
|
||||
engine.start()
|
||||
|
||||
// Trigger item update
|
||||
itemUpdateCallback!()
|
||||
|
||||
// Wait for async refresh
|
||||
await new Promise((resolve) => setTimeout(resolve, 50))
|
||||
|
||||
const cached = engine.lastFeed()
|
||||
expect(cached).not.toBeNull()
|
||||
expect(cached!.items).toHaveLength(1)
|
||||
|
||||
engine.stop()
|
||||
})
|
||||
|
||||
test("TTL resets after reactive update", async () => {
|
||||
const location = createLocationSource()
|
||||
const weather = createWeatherSource()
|
||||
|
||||
const engine = new FeedEngine({ cacheTtlMs: 100 }).register(location).register(weather)
|
||||
|
||||
engine.start()
|
||||
|
||||
// Initial reactive update
|
||||
location.simulateUpdate({ lat: 51.5, lng: -0.1 })
|
||||
await new Promise((resolve) => setTimeout(resolve, 50))
|
||||
|
||||
expect(engine.lastFeed()).not.toBeNull()
|
||||
|
||||
// Wait 70ms (total 120ms from first update, past original TTL)
|
||||
// but trigger another update at 50ms to reset TTL
|
||||
location.simulateUpdate({ lat: 52.0, lng: -0.2 })
|
||||
await new Promise((resolve) => setTimeout(resolve, 50))
|
||||
|
||||
// Should still be cached because TTL was reset by second update
|
||||
expect(engine.lastFeed()).not.toBeNull()
|
||||
|
||||
engine.stop()
|
||||
})
|
||||
|
||||
test("cacheTtlMs is configurable", async () => {
|
||||
const engine = new FeedEngine({ cacheTtlMs: 30 })
|
||||
const location = createLocationSource()
|
||||
location.simulateUpdate({ lat: 51.5, lng: -0.1 })
|
||||
|
||||
engine.register(location)
|
||||
await engine.refresh()
|
||||
|
||||
expect(engine.lastFeed()).not.toBeNull()
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 40))
|
||||
|
||||
expect(engine.lastFeed()).toBeNull()
|
||||
})
|
||||
|
||||
test("auto-refreshes on TTL interval after start", async () => {
|
||||
let fetchCount = 0
|
||||
const source: FeedSource = {
|
||||
id: "counter",
|
||||
...noActions,
|
||||
async fetchContext() {
|
||||
fetchCount++
|
||||
return null
|
||||
},
|
||||
async fetchItems() {
|
||||
return [
|
||||
{
|
||||
id: `item-${fetchCount}`,
|
||||
type: "test",
|
||||
priority: 0.5,
|
||||
timestamp: new Date(),
|
||||
data: {},
|
||||
},
|
||||
]
|
||||
},
|
||||
}
|
||||
|
||||
const engine = new FeedEngine({ cacheTtlMs: 50 }).register(source)
|
||||
engine.start()
|
||||
|
||||
// Wait for two TTL intervals to elapse
|
||||
await new Promise((resolve) => setTimeout(resolve, 120))
|
||||
|
||||
// Should have auto-refreshed at least twice
|
||||
expect(fetchCount).toBeGreaterThanOrEqual(2)
|
||||
expect(engine.lastFeed()).not.toBeNull()
|
||||
|
||||
engine.stop()
|
||||
})
|
||||
|
||||
test("stop cancels periodic refresh", async () => {
|
||||
let fetchCount = 0
|
||||
const source: FeedSource = {
|
||||
id: "counter",
|
||||
...noActions,
|
||||
async fetchContext() {
|
||||
fetchCount++
|
||||
return null
|
||||
},
|
||||
}
|
||||
|
||||
const engine = new FeedEngine({ cacheTtlMs: 50 }).register(source)
|
||||
engine.start()
|
||||
engine.stop()
|
||||
|
||||
const countAfterStop = fetchCount
|
||||
|
||||
// Wait past TTL
|
||||
await new Promise((resolve) => setTimeout(resolve, 80))
|
||||
|
||||
// No additional fetches after stop
|
||||
expect(fetchCount).toBe(countAfterStop)
|
||||
})
|
||||
|
||||
test("reactive update resets periodic refresh timer", async () => {
|
||||
let fetchCount = 0
|
||||
const location = createLocationSource()
|
||||
const countingWeather: FeedSource<WeatherFeedItem> = {
|
||||
id: "weather",
|
||||
dependencies: ["location"],
|
||||
...noActions,
|
||||
async fetchContext(ctx) {
|
||||
fetchCount++
|
||||
const loc = contextValue(ctx, LocationKey)
|
||||
if (!loc) return null
|
||||
return { [WeatherKey]: { temperature: 20, condition: "sunny" } }
|
||||
},
|
||||
async fetchItems(ctx) {
|
||||
const weather = contextValue(ctx, WeatherKey)
|
||||
if (!weather) return []
|
||||
return [
|
||||
{
|
||||
id: `weather-${Date.now()}`,
|
||||
type: "weather",
|
||||
priority: 0.5,
|
||||
timestamp: new Date(),
|
||||
data: { temperature: weather.temperature, condition: weather.condition },
|
||||
},
|
||||
]
|
||||
},
|
||||
}
|
||||
|
||||
const engine = new FeedEngine({ cacheTtlMs: 100 })
|
||||
.register(location)
|
||||
.register(countingWeather)
|
||||
|
||||
engine.start()
|
||||
|
||||
// At 40ms, push a reactive update — this resets the timer
|
||||
await new Promise((resolve) => setTimeout(resolve, 40))
|
||||
const countBeforeUpdate = fetchCount
|
||||
location.simulateUpdate({ lat: 51.5, lng: -0.1 })
|
||||
await new Promise((resolve) => setTimeout(resolve, 20))
|
||||
|
||||
// Reactive update triggered a fetch
|
||||
expect(fetchCount).toBeGreaterThan(countBeforeUpdate)
|
||||
const countAfterUpdate = fetchCount
|
||||
|
||||
// At 100ms from start (60ms after reactive update), the original
|
||||
// timer would have fired, but it was reset. No extra fetch yet.
|
||||
await new Promise((resolve) => setTimeout(resolve, 40))
|
||||
expect(fetchCount).toBe(countAfterUpdate)
|
||||
|
||||
engine.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -16,6 +16,14 @@ export interface FeedResult<TItem extends FeedItem = FeedItem> {
|
||||
|
||||
export type FeedSubscriber<TItem extends FeedItem = FeedItem> = (result: FeedResult<TItem>) => void
|
||||
|
||||
const DEFAULT_CACHE_TTL_MS = 300_000 // 5 minutes
|
||||
const MIN_CACHE_TTL_MS = 10 // prevent spin from zero/negative values
|
||||
|
||||
export interface FeedEngineConfig {
|
||||
/** Cache TTL in milliseconds. Default: 300_000 (5 minutes). Minimum: 10. */
|
||||
cacheTtlMs?: number
|
||||
}
|
||||
|
||||
interface SourceGraph {
|
||||
sources: Map<string, FeedSource>
|
||||
sorted: FeedSource[]
|
||||
@@ -59,6 +67,29 @@ export class FeedEngine<TItems extends FeedItem = FeedItem> {
|
||||
private cleanups: Array<() => void> = []
|
||||
private started = false
|
||||
|
||||
private readonly cacheTtlMs: number
|
||||
private cachedResult: FeedResult<TItems> | null = null
|
||||
private cachedAt: number | null = null
|
||||
private refreshTimer: ReturnType<typeof setTimeout> | null = null
|
||||
|
||||
constructor(config?: FeedEngineConfig) {
|
||||
this.cacheTtlMs = Math.max(config?.cacheTtlMs ?? DEFAULT_CACHE_TTL_MS, MIN_CACHE_TTL_MS)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the cached FeedResult if available and not expired.
|
||||
* Returns null if no refresh has completed or the cache TTL has elapsed.
|
||||
*/
|
||||
lastFeed(): FeedResult<TItems> | null {
|
||||
if (this.cachedResult === null || this.cachedAt === null) {
|
||||
return null
|
||||
}
|
||||
if (Date.now() - this.cachedAt > this.cacheTtlMs) {
|
||||
return null
|
||||
}
|
||||
return this.cachedResult
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a FeedSource. Invalidates the cached graph.
|
||||
*/
|
||||
@@ -124,7 +155,10 @@ export class FeedEngine<TItems extends FeedItem = FeedItem> {
|
||||
|
||||
this.context = context
|
||||
|
||||
return { context, items: items as TItems[], errors }
|
||||
const result: FeedResult<TItems> = { context, items: items as TItems[], errors }
|
||||
this.updateCache(result)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,7 +172,7 @@ export class FeedEngine<TItems extends FeedItem = FeedItem> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts reactive subscriptions on all sources.
|
||||
* Starts reactive subscriptions on all sources and begins periodic refresh.
|
||||
* Sources with onContextUpdate will trigger re-computation of dependents.
|
||||
*/
|
||||
start(): void {
|
||||
@@ -168,13 +202,16 @@ export class FeedEngine<TItems extends FeedItem = FeedItem> {
|
||||
this.cleanups.push(cleanup)
|
||||
}
|
||||
}
|
||||
|
||||
this.scheduleNextRefresh()
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops all reactive subscriptions.
|
||||
* Stops all reactive subscriptions and the periodic refresh timer.
|
||||
*/
|
||||
stop(): void {
|
||||
this.started = false
|
||||
this.cancelScheduledRefresh()
|
||||
for (const cleanup of this.cleanups) {
|
||||
cleanup()
|
||||
}
|
||||
@@ -279,11 +316,14 @@ export class FeedEngine<TItems extends FeedItem = FeedItem> {
|
||||
|
||||
items.sort((a, b) => b.priority - a.priority)
|
||||
|
||||
this.notifySubscribers({
|
||||
const result: FeedResult<TItems> = {
|
||||
context: this.context,
|
||||
items: items as TItems[],
|
||||
errors,
|
||||
})
|
||||
}
|
||||
this.updateCache(result)
|
||||
|
||||
this.notifySubscribers(result)
|
||||
}
|
||||
|
||||
private collectDependents(sourceId: string, graph: SourceGraph): string[] {
|
||||
@@ -307,11 +347,46 @@ export class FeedEngine<TItems extends FeedItem = FeedItem> {
|
||||
return graph.sorted.filter((s) => result.includes(s.id)).map((s) => s.id)
|
||||
}
|
||||
|
||||
private updateCache(result: FeedResult<TItems>): void {
|
||||
this.cachedResult = result
|
||||
this.cachedAt = Date.now()
|
||||
if (this.started) {
|
||||
this.scheduleNextRefresh()
|
||||
}
|
||||
}
|
||||
|
||||
private scheduleNextRefresh(): void {
|
||||
this.cancelScheduledRefresh()
|
||||
this.refreshTimer = setTimeout(() => {
|
||||
this.refresh()
|
||||
.then((result) => {
|
||||
this.notifySubscribers(result)
|
||||
})
|
||||
.catch(() => {
|
||||
// Periodic refresh errors are non-fatal; schedule next attempt
|
||||
if (this.started) {
|
||||
this.scheduleNextRefresh()
|
||||
}
|
||||
})
|
||||
}, this.cacheTtlMs)
|
||||
}
|
||||
|
||||
private cancelScheduledRefresh(): void {
|
||||
if (this.refreshTimer !== null) {
|
||||
clearTimeout(this.refreshTimer)
|
||||
this.refreshTimer = null
|
||||
}
|
||||
}
|
||||
|
||||
private scheduleRefresh(): void {
|
||||
// Simple immediate refresh for now - could add debouncing later
|
||||
this.refresh().then((result) => {
|
||||
this.notifySubscribers(result)
|
||||
})
|
||||
this.refresh()
|
||||
.then((result) => {
|
||||
this.notifySubscribers(result)
|
||||
})
|
||||
.catch(() => {
|
||||
// Reactive refresh errors are non-fatal
|
||||
})
|
||||
}
|
||||
|
||||
private notifySubscribers(result: FeedResult<TItems>): void {
|
||||
|
||||
@@ -13,7 +13,7 @@ export type { FeedItem } from "./feed"
|
||||
export type { FeedSource } from "./feed-source"
|
||||
|
||||
// Feed Engine
|
||||
export type { FeedResult, FeedSubscriber, SourceError } from "./feed-engine"
|
||||
export type { FeedEngineConfig, FeedResult, FeedSubscriber, SourceError } from "./feed-engine"
|
||||
export { FeedEngine } from "./feed-engine"
|
||||
|
||||
// =============================================================================
|
||||
|
||||
Reference in New Issue
Block a user