-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
76 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
export default function atomWithBroadcast<Value, Arg, Result>(read: (get: Getter) => Value, write: Write<Arg, Result>, broadcastChannelId: string) { | ||
const created = atom(read, (get, set, update: Arg | { | ||
__atom_update_type__: "message", | ||
update: Arg; | ||
}) => { | ||
let message = false; | ||
if (typeof update === "object") { | ||
if ("__atom_update_type__" in update) { | ||
if (update.__atom_update_type__ === "message") { | ||
write(get, set, update.update); | ||
message = true; | ||
} | ||
} | ||
} | ||
if (!message) { | ||
write(get, set, update as Arg); | ||
const channel = new BroadcastChannel(broadcastChannelId); | ||
channel.postMessage(update); | ||
} | ||
}); | ||
created.debugLabel = broadcastChannelId; | ||
created.onMount = setAtom => { | ||
const channel = new BroadcastChannel(broadcastChannelId); | ||
channel.addEventListener("message", event => { | ||
setAtom({ | ||
__atom_update_type__: "message", | ||
update: event.data | ||
}); | ||
}); | ||
}; | ||
return created; | ||
} | ||
import { | ||
Atom, | ||
WritableAtom, | ||
atom | ||
} from "jotai"; | ||
/** | ||
* @source jotai/atom.d.ts | ||
*/ | ||
type Getter = <Value>(atom: Atom<Value>) => Value; | ||
type Setter = <Value, Args extends unknown[], Result>(atom: WritableAtom<Value, Args, Result>, ...args: Args) => Result; | ||
type Write<Arg, Result> = (get: Getter, set: Setter, arg: Arg) => Result; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,19 @@ | ||
import { | ||
atom | ||
} from "jotai"; | ||
import setSetting from "./setSetting"; | ||
import settingReader from "./settingReader"; | ||
import atomWithBroadcast from "./atomWithBroadcast"; | ||
import atomWithInitialValue, { | ||
valueAtomReturn | ||
} from "./atomWithInitialValue"; | ||
import setSetting from "./setSetting"; | ||
import settingReader from "./settingReader"; | ||
export const emptySymbol: unique symbol = Symbol("This atom is empty, it's waiting a value."); | ||
const atomWithStorage = <setting = any>(id: string, name: string, empty: setting) => atomWithInitialValue((valueAtom: valueAtomReturn<setting>) => { | ||
const packagedAtom = atom(get => { | ||
const got = get(valueAtom); | ||
if (typeof got === "symbol") { | ||
const value = settingReader(id, empty); | ||
return value; | ||
} | ||
return got; | ||
}, (get, set, update: setting | { | ||
__atom_update_type__: "message", | ||
update: setting; | ||
}) => { | ||
let message = false; | ||
if (typeof update === "object") { | ||
if ("__atom_update_type__" in update) { | ||
if (update.__atom_update_type__ === "message") { | ||
set(valueAtom, update.update); | ||
message = true; | ||
} | ||
} | ||
} | ||
if (!message) { | ||
set(valueAtom, update as setting); | ||
setSetting(id, name, update); | ||
const channel = new BroadcastChannel(`atomWithStorage-${id}`); | ||
channel.postMessage(update); | ||
} | ||
}); | ||
packagedAtom.debugLabel = id; | ||
packagedAtom.onMount = setAtom => { | ||
const channel = new BroadcastChannel(`atomWithStorage-${id}`); | ||
channel.addEventListener("message", event => { | ||
setAtom({ | ||
__atom_update_type__: "message", | ||
update: event.data | ||
}); | ||
}); | ||
}; | ||
return packagedAtom; | ||
})[0]; | ||
const atomWithStorage = <setting = any>(id: string, name: string, empty: setting) => atomWithInitialValue((valueAtom: valueAtomReturn<setting>) => atomWithBroadcast(get => { | ||
const got = get(valueAtom); | ||
if (got === emptySymbol) { | ||
const value = settingReader(id, empty); | ||
return value; | ||
} | ||
return got; | ||
}, (get, set, update: setting) => { | ||
set(valueAtom, update); | ||
setSetting(id, name, update); | ||
}, `storagedAtom-${id}`))[0]; | ||
export default atomWithStorage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters