@rific/splash-gate 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +69 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +64 -0
- package/dist/index.mjs +27 -0
- package/package.json +85 -0
- package/src/__mocks__/expo-splash-screen.ts +3 -0
- package/src/index.ts +2 -0
- package/src/splashGate.ts +60 -0
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# @rific/splash-gate
|
|
2
|
+
|
|
3
|
+
Name every async condition your Expo app's first screen actually depends on (a loaded theme, a loaded icon font, a hydrated saved preference, an auth check) and hold the splash screen up until all of them report ready. One shared, tested utility instead of a fresh ad hoc module-scope gate copy-pasted into every project.
|
|
4
|
+
|
|
5
|
+
## Why
|
|
6
|
+
|
|
7
|
+
`SplashScreen.preventAutoHideAsync()` only holds the splash up. It's on you to call `hideAsync()` at the right moment. Call it too early (e.g. the instant your theme loads) and anything else your first screen depends on but forgot to wait for, an icon font, a hydrated preference, pops in visibly a moment after the splash lifts. `createSplashGate` makes every condition explicit up front, so nothing can be forgotten silently.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install @rific/splash-gate
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
// splashGate.ts, created once, at module scope, listing every condition this app's first
|
|
19
|
+
// screen actually depends on
|
|
20
|
+
import { createSplashGate } from '@rific/splash-gate'
|
|
21
|
+
|
|
22
|
+
export const { markReady, useReady, pendingGates } = createSplashGate(['theme', 'fonts', 'keyboardLayout'])
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
// Theme.tsx
|
|
27
|
+
import * as SplashScreen from 'expo-splash-screen'
|
|
28
|
+
import { useFonts } from 'expo-font'
|
|
29
|
+
import { markReady, useReady } from './splashGate'
|
|
30
|
+
|
|
31
|
+
SplashScreen.preventAutoHideAsync()
|
|
32
|
+
|
|
33
|
+
export const Theme = ({ children }) => {
|
|
34
|
+
const [fontsLoaded] = useFonts({ ... })
|
|
35
|
+
useReady('fonts', fontsLoaded)
|
|
36
|
+
|
|
37
|
+
const onThemeReady = () => markReady('theme') // a one-shot callback, not a boolean, call directly
|
|
38
|
+
|
|
39
|
+
return <Provider onReady={onThemeReady}>{children}</Provider>
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
// KeyboardLayoutProvider.tsx
|
|
45
|
+
import { useReady } from './splashGate'
|
|
46
|
+
|
|
47
|
+
const [loaded, setLoaded] = useState(false)
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
void AsyncStorage.getItem(STORAGE_KEY).then((stored) => {
|
|
50
|
+
if (stored) setLayoutState(stored)
|
|
51
|
+
setLoaded(true) // mark loaded whether or not a saved value was actually found
|
|
52
|
+
})
|
|
53
|
+
}, [])
|
|
54
|
+
useReady('keyboardLayout', loaded)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The splash screen hides exactly once, the moment `theme`, `fonts`, and `keyboardLayout` have all reported ready, in whatever order they actually resolve.
|
|
58
|
+
|
|
59
|
+
## API
|
|
60
|
+
|
|
61
|
+
### `createSplashGate(gates)`
|
|
62
|
+
|
|
63
|
+
Takes an array of gate names (typically `as const` for literal-type safety) and returns:
|
|
64
|
+
|
|
65
|
+
- `markReady(gate)`: marks one gate ready. Safe to call more than once for the same gate, and safe in any order. Hides the splash screen exactly once, once every gate is ready. For a gate that only resolves via a one-shot callback (a library's own `onReady` prop, a promise `.then`), call this directly there.
|
|
66
|
+
- `useReady(gate, ready)`: for a gate whose readiness is already a plain boolean (state from a hook like `useFonts`, or your own derived condition), marks it ready once `ready` becomes `true`. Bound to this gate instance, so there's nothing to pass but the two things that actually vary at each call site.
|
|
67
|
+
- `pendingGates()`: the gate names still outstanding, for a debug log during development.
|
|
68
|
+
|
|
69
|
+
Call this once, at module scope, not inside a component body, or you'll get a fresh gate (and a fresh splash-hide race) on every render.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
type SplashGate<T extends string> = {
|
|
2
|
+
markReady: (gate: T) => void;
|
|
3
|
+
useReady: (gate: T, ready: boolean) => void;
|
|
4
|
+
pendingGates: () => T[];
|
|
5
|
+
};
|
|
6
|
+
declare function createSplashGate<T extends string>(gates: readonly T[]): SplashGate<T>;
|
|
7
|
+
|
|
8
|
+
export { type SplashGate, createSplashGate };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
type SplashGate<T extends string> = {
|
|
2
|
+
markReady: (gate: T) => void;
|
|
3
|
+
useReady: (gate: T, ready: boolean) => void;
|
|
4
|
+
pendingGates: () => T[];
|
|
5
|
+
};
|
|
6
|
+
declare function createSplashGate<T extends string>(gates: readonly T[]): SplashGate<T>;
|
|
7
|
+
|
|
8
|
+
export { type SplashGate, createSplashGate };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
createSplashGate: () => createSplashGate
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
|
|
37
|
+
// src/splashGate.ts
|
|
38
|
+
var SplashScreen = __toESM(require("expo-splash-screen"));
|
|
39
|
+
var import_react = require("react");
|
|
40
|
+
function createSplashGate(gates) {
|
|
41
|
+
const pending = new Set(gates);
|
|
42
|
+
let hidden = false;
|
|
43
|
+
const tryHide = () => {
|
|
44
|
+
if (hidden || pending.size > 0) return;
|
|
45
|
+
hidden = true;
|
|
46
|
+
void SplashScreen.hideAsync();
|
|
47
|
+
};
|
|
48
|
+
const markReady = (gate) => {
|
|
49
|
+
pending.delete(gate);
|
|
50
|
+
tryHide();
|
|
51
|
+
};
|
|
52
|
+
const useReady = (gate, ready) => {
|
|
53
|
+
(0, import_react.useEffect)(() => {
|
|
54
|
+
if (ready) markReady(gate);
|
|
55
|
+
}, [ready, gate]);
|
|
56
|
+
};
|
|
57
|
+
const pendingGates = () => Array.from(pending);
|
|
58
|
+
tryHide();
|
|
59
|
+
return { markReady, useReady, pendingGates };
|
|
60
|
+
}
|
|
61
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
62
|
+
0 && (module.exports = {
|
|
63
|
+
createSplashGate
|
|
64
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// src/splashGate.ts
|
|
2
|
+
import * as SplashScreen from "expo-splash-screen";
|
|
3
|
+
import { useEffect } from "react";
|
|
4
|
+
function createSplashGate(gates) {
|
|
5
|
+
const pending = new Set(gates);
|
|
6
|
+
let hidden = false;
|
|
7
|
+
const tryHide = () => {
|
|
8
|
+
if (hidden || pending.size > 0) return;
|
|
9
|
+
hidden = true;
|
|
10
|
+
void SplashScreen.hideAsync();
|
|
11
|
+
};
|
|
12
|
+
const markReady = (gate) => {
|
|
13
|
+
pending.delete(gate);
|
|
14
|
+
tryHide();
|
|
15
|
+
};
|
|
16
|
+
const useReady = (gate, ready) => {
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
if (ready) markReady(gate);
|
|
19
|
+
}, [ready, gate]);
|
|
20
|
+
};
|
|
21
|
+
const pendingGates = () => Array.from(pending);
|
|
22
|
+
tryHide();
|
|
23
|
+
return { markReady, useReady, pendingGates };
|
|
24
|
+
}
|
|
25
|
+
export {
|
|
26
|
+
createSplashGate
|
|
27
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rific/splash-gate",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Name every async condition an Expo app's first screen depends on (theme, fonts, saved preferences, auth, ...) and hold the splash screen up until all of them report ready — instead of hand-rolling a fresh ad hoc gate per project",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"expo",
|
|
7
|
+
"expo-splash-screen",
|
|
8
|
+
"splash-screen",
|
|
9
|
+
"app-loading",
|
|
10
|
+
"font-loading"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/jayrdeaton/Expo-Splash-Gate#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/jayrdeaton/Expo-Splash-Gate/issues"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/jayrdeaton/Expo-Splash-Gate.git"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"author": "Jay Deaton",
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"type": "commonjs",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"react-native": "./dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"import": "./dist/index.mjs",
|
|
29
|
+
"require": "./dist/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"main": "dist/index.js",
|
|
33
|
+
"module": "dist/index.mjs",
|
|
34
|
+
"types": "dist/index.d.ts",
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"src",
|
|
38
|
+
"!src/__tests__"
|
|
39
|
+
],
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
42
|
+
"build:watch": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
43
|
+
"fix": "eslint --fix",
|
|
44
|
+
"lint": "eslint",
|
|
45
|
+
"prepublishOnly": "npm run build",
|
|
46
|
+
"release": "git push --follow-tags",
|
|
47
|
+
"release:major": "npm version major && git push --follow-tags",
|
|
48
|
+
"release:minor": "npm version minor && git push --follow-tags",
|
|
49
|
+
"release:patch": "npm version patch && git push --follow-tags",
|
|
50
|
+
"test": "jest",
|
|
51
|
+
"test:watch": "jest --watchAll",
|
|
52
|
+
"typecheck": "tsc --noEmit",
|
|
53
|
+
"preversion": "npm run lint && npm test && npm run build"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@testing-library/dom": "^10.4.1",
|
|
57
|
+
"@testing-library/react": "^16.3.2",
|
|
58
|
+
"@types/jest": "^30.0.0",
|
|
59
|
+
"@types/react": "^19.0.0",
|
|
60
|
+
"@typescript-eslint/parser": "^8.59.3",
|
|
61
|
+
"eslint": "^9.39.4",
|
|
62
|
+
"eslint-config-prettier": "^10.1.8",
|
|
63
|
+
"eslint-plugin-package-json": "^1.0.0",
|
|
64
|
+
"eslint-plugin-prettier": "^5.5.5",
|
|
65
|
+
"eslint-plugin-simple-import-sort": "^13.0.0",
|
|
66
|
+
"expo-splash-screen": "^57.0.5",
|
|
67
|
+
"jest": "^30.4.2",
|
|
68
|
+
"jest-environment-jsdom": "^30.4.1",
|
|
69
|
+
"prettier": "^3.8.3",
|
|
70
|
+
"react": "^19.2.6",
|
|
71
|
+
"react-dom": "^19.0.0",
|
|
72
|
+
"ts-jest": "^29.4.9",
|
|
73
|
+
"ts-node": "^10.9.2",
|
|
74
|
+
"tsup": "^8.0.0",
|
|
75
|
+
"typescript": "^6.0.3",
|
|
76
|
+
"typescript-eslint": "^8.59.3"
|
|
77
|
+
},
|
|
78
|
+
"peerDependencies": {
|
|
79
|
+
"expo-splash-screen": ">=0.30.0",
|
|
80
|
+
"react": ">=18.0.0"
|
|
81
|
+
},
|
|
82
|
+
"publishConfig": {
|
|
83
|
+
"access": "public"
|
|
84
|
+
}
|
|
85
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import * as SplashScreen from 'expo-splash-screen'
|
|
2
|
+
import { useEffect } from 'react'
|
|
3
|
+
|
|
4
|
+
export type SplashGate<T extends string> = {
|
|
5
|
+
// Marks one named gate ready. Safe to call more than once for the same gate (a no-op after the
|
|
6
|
+
// first) and safe to call for every gate in any order: the splash only ever hides once every
|
|
7
|
+
// gate this instance was created with has been marked, and only ever hides once. For a gate that
|
|
8
|
+
// only resolves via a one-shot callback (a library's own `onReady` prop, a promise `.then`),
|
|
9
|
+
// call this directly there. For a gate that's already a plain boolean (state from a hook like
|
|
10
|
+
// `useFonts`, or your own derived condition), useReady below is that same call written once.
|
|
11
|
+
markReady: (gate: T) => void
|
|
12
|
+
// Marks `gate` ready once `ready` becomes true. Bound to this gate instance, so there's nothing
|
|
13
|
+
// to pass but the two things that actually vary at the call site.
|
|
14
|
+
useReady: (gate: T, ready: boolean) => void
|
|
15
|
+
// Gate names still outstanding, in no particular order. For a debug log or a "why is this still
|
|
16
|
+
// up" check during development, not something app code should need to branch on.
|
|
17
|
+
pendingGates: () => T[]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// One named condition per thing your first screen actually depends on (a loaded theme, a loaded
|
|
21
|
+
// font, a hydrated preference, a resolved auth check, ...) instead of hand-rolling a fresh ad hoc
|
|
22
|
+
// module-scope gate per project. Call this once, up front, with every gate the app needs, then
|
|
23
|
+
// call `markReady`/`useReady` for each one from wherever it actually resolves. The splash stays up
|
|
24
|
+
// (you're still responsible for `SplashScreen.preventAutoHideAsync()` yourself, before this runs)
|
|
25
|
+
// until every named gate has reported in, then it hides itself exactly once, so nothing your
|
|
26
|
+
// app's first screen shows can ever be caught mid-load, wrong-then-corrected, a beat behind the
|
|
27
|
+
// rest.
|
|
28
|
+
export function createSplashGate<T extends string>(gates: readonly T[]): SplashGate<T> {
|
|
29
|
+
const pending = new Set<T>(gates)
|
|
30
|
+
let hidden = false
|
|
31
|
+
|
|
32
|
+
const tryHide = () => {
|
|
33
|
+
if (hidden || pending.size > 0) return
|
|
34
|
+
hidden = true
|
|
35
|
+
void SplashScreen.hideAsync()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const markReady = (gate: T) => {
|
|
39
|
+
pending.delete(gate)
|
|
40
|
+
tryHide()
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const useReady = (gate: T, ready: boolean) => {
|
|
44
|
+
// markReady isn't in this dep array. It's this closure's own stable reference (one per
|
|
45
|
+
// createSplashGate call), not a per-render value, so there's nothing to gain by re-running
|
|
46
|
+
// this effect if it somehow changed identity.
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
if (ready) markReady(gate)
|
|
49
|
+
}, [ready, gate])
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const pendingGates = () => Array.from(pending)
|
|
53
|
+
|
|
54
|
+
// Nothing to wait for (called with an empty list, or every gate was already satisfied some
|
|
55
|
+
// other way). Hide right away instead of leaving the splash up forever with nothing left that
|
|
56
|
+
// could ever call markReady again.
|
|
57
|
+
tryHide()
|
|
58
|
+
|
|
59
|
+
return { markReady, useReady, pendingGates }
|
|
60
|
+
}
|