@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
108 lines • 3.79 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.announcementMachine = void 0;
const xstate_1 = require("xstate");
const intersection_1 = __importDefault(require("lodash/intersection"));
const initialState = {
cache: {},
seenIds: [],
allIds: [],
error: null,
lastUpdateTime: null,
isLoading: false,
};
exports.announcementMachine = (0, xstate_1.createMachine)({
id: "announcement",
initial: "initializing",
context: initialState,
states: {
initializing: {
invoke: {
src: "loadData",
onDone: {
target: "updating",
actions: (0, xstate_1.assign)(({ event }) => {
const { announcements, seenIds, lastUpdateTime } = event.output;
const cache = {};
announcements.forEach(announcement => {
cache[announcement.uuid] = announcement;
});
const allIds = Object.keys(cache);
return {
allIds,
cache,
seenIds,
lastUpdateTime,
};
}),
},
},
},
idle: {
after: {
AUTO_UPDATE_DELAY: {
target: "updating",
},
},
on: {
UPDATE_DATA: {
target: "updating",
actions: (0, xstate_1.assign)({
isLoading: true,
error: null,
}),
},
},
},
updating: {
invoke: {
src: "fetchData",
input: ({ context }) => ({ allIds: context.allIds, cache: context.cache }),
onDone: {
target: "idle",
actions: [
(0, xstate_1.assign)(({ context, event }) => {
const { announcements, updateTime } = event.output;
const cache = {};
announcements.forEach(announcement => {
cache[announcement.uuid] = announcement;
});
const allIds = Object.keys(cache);
return {
cache,
seenIds: (0, intersection_1.default)(allIds, context.seenIds),
allIds,
lastUpdateTime: updateTime,
isLoading: false,
error: null,
};
}),
"saveData",
],
},
onError: {
target: "idle",
actions: (0, xstate_1.assign)(({ event }) => ({
error: event.error,
})),
},
},
},
},
on: {
SET_AS_SEEN: {
guard: ({ context, event }) => !context.seenIds.includes(event.seenId),
actions: ["setAsSeen", "saveData", "emitNewAnnouncement"],
},
},
}, {
actions: {
setAsSeen: (0, xstate_1.assign)(({ context, event }) => ({
seenIds: [...context.seenIds, event.seenId],
})),
},
});
//# sourceMappingURL=machine.js.map