objcjs-types 0.5.0 → 0.5.1
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/dist/bind.js +4 -0
- package/dist/helpers.d.ts +26 -0
- package/dist/helpers.js +44 -0
- package/generator/index.ts +1 -1
- package/generator/templates/bind.ts +4 -0
- package/generator/templates/helpers.ts +53 -0
- package/package.json +2 -2
package/dist/bind.js
CHANGED
|
@@ -90,6 +90,10 @@ export function isKindOfClass(obj, cls) {
|
|
|
90
90
|
*/
|
|
91
91
|
export function _bindClass(lib, name) {
|
|
92
92
|
const cls = lib[name];
|
|
93
|
+
if (!cls) {
|
|
94
|
+
// Very unlikely to happen, but just in case, return undefined.
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
93
97
|
Object.defineProperty(cls, Symbol.hasInstance, {
|
|
94
98
|
value: (obj) => _isKindOfClass(obj, cls)
|
|
95
99
|
});
|
package/dist/helpers.d.ts
CHANGED
|
@@ -27,4 +27,30 @@ export declare function NSStringFromString(str: string): _NSString;
|
|
|
27
27
|
export declare function options<T extends number>(...values: T[]): T;
|
|
28
28
|
export declare function NSArrayFromObjects(objects: NobjcObject[]): _NSArray;
|
|
29
29
|
export declare function NSDictionaryFromKeysAndValues(keys: NobjcObject[], values: NobjcObject[]): _NSDictionary;
|
|
30
|
+
/**
|
|
31
|
+
* Returns the key name for a given numeric value in a const enum-like object.
|
|
32
|
+
*
|
|
33
|
+
* @param enumObj - A const object mapping string keys to numeric values.
|
|
34
|
+
* @param value - The numeric value to look up.
|
|
35
|
+
* @returns The matching key name, or `undefined` if not found.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* const state = enumFromValue(
|
|
40
|
+
* ASAuthorizationWebBrowserPublicKeyCredentialManagerAuthorizationState,
|
|
41
|
+
* rawValue
|
|
42
|
+
* ); // e.g. "Authorized" | "Denied" | "NotDetermined" | undefined
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare function enumFromValue<T extends Record<string, number>>(enumObj: T, value: number): keyof T | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* Converts a callback-based function with a single result parameter into a Promise.
|
|
48
|
+
*
|
|
49
|
+
* @template TArgs - The argument types of the original function (excluding the callback)
|
|
50
|
+
* @template TResult - The type of the result passed to the callback
|
|
51
|
+
* @param func - A function that takes arguments and a callback with a single result parameter
|
|
52
|
+
* @param args - The arguments to pass to the function (excluding the callback)
|
|
53
|
+
* @returns A Promise that resolves with the result passed to the callback
|
|
54
|
+
*/
|
|
55
|
+
export declare function makePromise1Result<TArgs extends any[], TResult>(func: (...args: [...TArgs, (result: TResult) => void]) => void, ...args: TArgs): Promise<TResult>;
|
|
30
56
|
export { isKindOfClass } from "./bind.js";
|
package/dist/helpers.js
CHANGED
|
@@ -54,4 +54,48 @@ export function NSDictionaryFromKeysAndValues(keys, values) {
|
|
|
54
54
|
const valuesArray = NSArrayFromObjects(values);
|
|
55
55
|
return NSDictionary.dictionaryWithObjects$forKeys$(valuesArray, keysArray);
|
|
56
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Returns the key name for a given numeric value in a const enum-like object.
|
|
59
|
+
*
|
|
60
|
+
* @param enumObj - A const object mapping string keys to numeric values.
|
|
61
|
+
* @param value - The numeric value to look up.
|
|
62
|
+
* @returns The matching key name, or `undefined` if not found.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* const state = enumFromValue(
|
|
67
|
+
* ASAuthorizationWebBrowserPublicKeyCredentialManagerAuthorizationState,
|
|
68
|
+
* rawValue
|
|
69
|
+
* ); // e.g. "Authorized" | "Denied" | "NotDetermined" | undefined
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export function enumFromValue(enumObj, value) {
|
|
73
|
+
return Object.keys(enumObj).find((key) => enumObj[key] === value);
|
|
74
|
+
}
|
|
75
|
+
function PromiseWithResolvers() {
|
|
76
|
+
let resolve;
|
|
77
|
+
let reject;
|
|
78
|
+
const promise = new Promise((res, rej) => {
|
|
79
|
+
resolve = res;
|
|
80
|
+
reject = rej;
|
|
81
|
+
});
|
|
82
|
+
return { promise, resolve: resolve, reject: reject };
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Converts a callback-based function with a single result parameter into a Promise.
|
|
86
|
+
*
|
|
87
|
+
* @template TArgs - The argument types of the original function (excluding the callback)
|
|
88
|
+
* @template TResult - The type of the result passed to the callback
|
|
89
|
+
* @param func - A function that takes arguments and a callback with a single result parameter
|
|
90
|
+
* @param args - The arguments to pass to the function (excluding the callback)
|
|
91
|
+
* @returns A Promise that resolves with the result passed to the callback
|
|
92
|
+
*/
|
|
93
|
+
export function makePromise1Result(func, ...args) {
|
|
94
|
+
const { promise, resolve } = PromiseWithResolvers();
|
|
95
|
+
const callback = (result) => {
|
|
96
|
+
resolve(result);
|
|
97
|
+
};
|
|
98
|
+
func(...args, callback);
|
|
99
|
+
return promise;
|
|
100
|
+
}
|
|
57
101
|
export { isKindOfClass } from "./bind.js";
|
package/generator/index.ts
CHANGED
|
@@ -329,7 +329,7 @@ async function main(): Promise<void> {
|
|
|
329
329
|
// The actual per-task bottleneck is clang execution + JSON.parse (~95% of
|
|
330
330
|
// task time), not the AST walk passes (~5%). Any Xcode-capable Mac with
|
|
331
331
|
// 32+GB RAM handles 8 workers comfortably.
|
|
332
|
-
const cpuCount = navigator.hardwareConcurrency
|
|
332
|
+
const cpuCount = navigator.hardwareConcurrency || 4;
|
|
333
333
|
const poolSize = Math.min(cpuCount, 8);
|
|
334
334
|
const pool = new WorkerPool(poolSize);
|
|
335
335
|
const totalBatchTasks = batchTasks.length;
|
|
@@ -93,6 +93,10 @@ export function isKindOfClass<T extends NobjcObject>(obj: NobjcObject, cls: any)
|
|
|
93
93
|
*/
|
|
94
94
|
export function _bindClass<T>(lib: any, name: string): T {
|
|
95
95
|
const cls = lib[name];
|
|
96
|
+
if (!cls) {
|
|
97
|
+
// Very unlikely to happen, but just in case, return undefined.
|
|
98
|
+
return undefined as unknown as T;
|
|
99
|
+
}
|
|
96
100
|
Object.defineProperty(cls, Symbol.hasInstance, {
|
|
97
101
|
value: (obj: any) => _isKindOfClass(obj, cls)
|
|
98
102
|
});
|
|
@@ -67,4 +67,57 @@ export function NSDictionaryFromKeysAndValues(keys: NobjcObject[], values: Nobjc
|
|
|
67
67
|
return NSDictionary.dictionaryWithObjects$forKeys$(valuesArray, keysArray);
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Returns the key name for a given numeric value in a const enum-like object.
|
|
72
|
+
*
|
|
73
|
+
* @param enumObj - A const object mapping string keys to numeric values.
|
|
74
|
+
* @param value - The numeric value to look up.
|
|
75
|
+
* @returns The matching key name, or `undefined` if not found.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* const state = enumFromValue(
|
|
80
|
+
* ASAuthorizationWebBrowserPublicKeyCredentialManagerAuthorizationState,
|
|
81
|
+
* rawValue
|
|
82
|
+
* ); // e.g. "Authorized" | "Denied" | "NotDetermined" | undefined
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export function enumFromValue<T extends Record<string, number>>(enumObj: T, value: number): keyof T | undefined {
|
|
86
|
+
return (Object.keys(enumObj) as (keyof T)[]).find((key) => enumObj[key] === value);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function PromiseWithResolvers<T = void>(): {
|
|
90
|
+
promise: Promise<T>;
|
|
91
|
+
resolve: (value: T | PromiseLike<T>) => void;
|
|
92
|
+
reject: (reason?: unknown) => void;
|
|
93
|
+
} {
|
|
94
|
+
let resolve: (value: T | PromiseLike<T>) => void;
|
|
95
|
+
let reject: (reason?: unknown) => void;
|
|
96
|
+
const promise = new Promise<T>((res, rej) => {
|
|
97
|
+
resolve = res;
|
|
98
|
+
reject = rej;
|
|
99
|
+
});
|
|
100
|
+
return { promise, resolve: resolve!, reject: reject! };
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Converts a callback-based function with a single result parameter into a Promise.
|
|
104
|
+
*
|
|
105
|
+
* @template TArgs - The argument types of the original function (excluding the callback)
|
|
106
|
+
* @template TResult - The type of the result passed to the callback
|
|
107
|
+
* @param func - A function that takes arguments and a callback with a single result parameter
|
|
108
|
+
* @param args - The arguments to pass to the function (excluding the callback)
|
|
109
|
+
* @returns A Promise that resolves with the result passed to the callback
|
|
110
|
+
*/
|
|
111
|
+
export function makePromise1Result<TArgs extends any[], TResult>(
|
|
112
|
+
func: (...args: [...TArgs, (result: TResult) => void]) => void,
|
|
113
|
+
...args: TArgs
|
|
114
|
+
): Promise<TResult> {
|
|
115
|
+
const { promise, resolve } = PromiseWithResolvers<TResult>();
|
|
116
|
+
const callback = (result: TResult) => {
|
|
117
|
+
resolve(result);
|
|
118
|
+
};
|
|
119
|
+
func(...args, callback);
|
|
120
|
+
return promise;
|
|
121
|
+
}
|
|
122
|
+
|
|
70
123
|
export { isKindOfClass } from "./bind.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "objcjs-types",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "Auto-generated TypeScript type declarations for macOS Objective-C frameworks",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
"devDependencies": {
|
|
70
70
|
"@types/bun": "latest",
|
|
71
71
|
"@typescript/native-preview": "^7.0.0-dev.20260218.1",
|
|
72
|
-
"objc-js": "^1.3.
|
|
72
|
+
"objc-js": "^1.3.1",
|
|
73
73
|
"prettier": "^3.8.1"
|
|
74
74
|
},
|
|
75
75
|
"trustedDependencies": [
|