Skip to content

Commit

Permalink
aplly typings of global.d.ts to files
Browse files Browse the repository at this point in the history
  • Loading branch information
Auge19 committed Jan 22, 2025
1 parent 594efcd commit 1695010
Show file tree
Hide file tree
Showing 25 changed files with 142 additions and 118 deletions.
122 changes: 66 additions & 56 deletions global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,97 +87,104 @@ declare global {

//#region subscribables/dependencyDetection.js

export interface ComputedContext {
getDependenciesCount(): number;
getDependencies(): Subscribable[];
isInitial(): boolean;
registerDependency(subscribable: Subscribable): void;
}
// transfered types to dependencyDetection.ts
// export interface ComputedContext {
// getDependenciesCount(): number;
// getDependencies(): Subscribable[];
// isInitial(): boolean;
// registerDependency(subscribable: Subscribable): void;
// }

export const computedContext: ComputedContext;
// export const computedContext: ComputedContext;

/**
* Executes a function and returns the result, while disabling depdendency tracking
* @param callback - the function to execute without dependency tracking
* @param callbackTarget - the `this` binding for `callback`
* @param callbackArgs - the args to provide to `callback`
*/
export function ignoreDependencies<Return, Target, Args extends any[]>(
callback: (this: Target, ...args: Args) => Return,
callbackTarget?: Target,
callbackArgs?: Args
): Return;
// types transfered to dependencyDetection.ts
// export function ignoreDependencies<Return, Target, Args extends any[]>(
// callback: (this: Target, ...args: Args) => Return,
// callbackTarget?: Target,
// callbackArgs?: Args
// ): Return;

//#endregion

//#region subscribables/extenders.js

export type RateLimitMethod = (callback: () => void, timeout: number, options: any) => (() => void);
// no usage anymore
// export type RateLimitMethod = (callback: () => void, timeout: number, options: any) => (() => void);

export interface RateLimitOptions {
timeout: number;
method?: "notifyAtFixedRate" | "notifyWhenChangesStop" | RateLimitMethod;
[option: string]: any;
}
// export interface RateLimitOptions {
// timeout: number;
// method?: "notifyAtFixedRate" | "notifyWhenChangesStop" | RateLimitMethod;
// [option: string]: any;
// }

export interface ExtendersOptions<T = any> {
trackArrayChanges: true | utils.CompareArraysOptions;
throttle: number;
rateLimit: number | RateLimitOptions;
deferred: true;
notify: "always" | any;
}
// types transfered to extenders.ts and throttleExpanders.ts
// export interface ExtendersOptions<T = any> {
// trackArrayChanges: true | utils.CompareArraysOptions;
// throttle: number;
// rateLimit: number | RateLimitOptions;
// deferred: true;
// notify: "always" | any;
// }

export interface Extender<T extends Subscribable = any, O = any> {
(target: T, options: O): T;
}
// export interface Extender<T extends Subscribable = any, O = any> {
// (target: T, options: O): T;
// }

type AsExtenders<T> = { [P in keyof T]: Extender<Subscribable, T[P]> }
// type AsExtenders<T> = { [P in keyof T]: Extender<Subscribable, T[P]> }

export interface Extenders<T> extends AsExtenders<ExtendersOptions<T>> {
[name: string]: Extender;
}
// export interface Extenders<T> extends AsExtenders<ExtendersOptions<T>> {
// [name: string]: Extender;
// }

export interface ObservableExtenderOptions<T> extends Partial<ExtendersOptions<T>> { }
// export interface ObservableExtenderOptions<T> extends Partial<ExtendersOptions<T>> { }

export const extenders: Extenders<any>;
// export const extenders: Extenders<any>;

//#endregion

//#region subscribables/mappingHelpers.js

export type Unwrapped<T> = T extends ko.ObservableArray<infer R>
? Unwrapped<R>[]
: T extends ko.Subscribable<infer R>
? (
R extends ko.Subscribable
? unknown
: R extends Record<any, any>
? { [P in keyof R]: Unwrapped<R[P]> }
: R
)
: T extends Date | RegExp | Function
? T
: T extends Record<any, any>
? { [P in keyof T]: Unwrapped<T[P]> }
: T

export function toJS<T>(rootObject: T): Unwrapped<T>;
export function toJSON(rootObject: any, replacer?: Function, space?: number): string;
// transfered to mappingHelpers.ts
// export type Unwrapped<T> = T extends ko.ObservableArray<infer R>
// ? Unwrapped<R>[]
// : T extends ko.Subscribable<infer R>
// ? (
// R extends ko.Subscribable
// ? unknown
// : R extends Record<any, any>
// ? { [P in keyof R]: Unwrapped<R[P]> }
// : R
// )
// : T extends Date | RegExp | Function
// ? T
// : T extends Record<any, any>
// ? { [P in keyof T]: Unwrapped<T[P]> }
// : T

// export function toJS<T>(rootObject: T): Unwrapped<T>;
// export function toJSON(rootObject: any, replacer?: Function, space?: number): string;

//#endregion

//#region subscribables/observableUtils.js

export function when<T, TTarget = void>(predicate: ComputedReadFunction<T, TTarget>, callback: SubscriptionCallback<T, TTarget>, context?: TTarget): Subscription;
export function when<T>(predicate: ComputedReadFunction<T, void>): Promise<T>;
// types to subscribable
// export function when<T, TTarget = void>(predicate: ComputedReadFunction<T, TTarget>, callback: SubscriptionCallback<T, TTarget>, context?: TTarget): Subscription;
// export function when<T>(predicate: ComputedReadFunction<T, void>): Promise<T>;

//#endregion

//#region binding/bindingAttributeSyntax.js

export type BindingAccessors = { [name: string]: Function; };

// usage in applyBindings, BindingHandler, event, checked, options
export interface AllBindings {
(): any;

Expand All @@ -186,8 +193,10 @@ declare global {

has(name: string): boolean;
}
export type BindingHandlerControlsDescendant = { controlsDescendantBindings: boolean; }
export type BindingHandlerAddBinding = (name: string, value: any) => void;
// transfered to LegacyBindingHandler.ts
// export type BindingHandlerControlsDescendant = { controlsDescendantBindings: boolean; }
// export type BindingHandlerAddBinding = (name: string, value: any) => void;
// used as Base for all BindingHandlers
export interface BindingHandler<T = any> {
after?: string[];
init?: (element: any, valueAccessor: () => T, allBindings: AllBindings, viewModel: any, bindingContext: BindingContext<any>) => void | BindingHandlerControlsDescendant;
Expand All @@ -200,6 +209,7 @@ declare global {
[name: string]: BindingHandler;
}

// global usage. defined in bindingContext.ts
export interface BindingContext<T = any> {
ko: any; // typeof ko;

Expand Down
4 changes: 2 additions & 2 deletions packages/bind/src/BindingHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { isWriteableObservable } from '@tko/observable'
import { LifeCycle } from '@tko/lifecycle'

export class BindingHandler extends LifeCycle {
$context: any
$context: any // most likly BindingContext but params must be typed first
$element: HTMLElement
$data: any
bindingCompletion: any
valueAccessor: Function
completeBinding: any
allBindings: any
allBindings: AllBindings

constructor (params) {
super()
Expand Down
2 changes: 1 addition & 1 deletion packages/bind/src/DescendantBindingHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { AsyncBindingHandler } from './BindingHandler'
export class DescendantBindingHandler extends AsyncBindingHandler {
get controlsDescendants () { return true }

async applyBindingsToDescendants (childContext: any, callback?: Function) {
async applyBindingsToDescendants (childContext: BindingContext, callback?: Function) {
const bindingResult = applyBindingsToDescendants(childContext, this.$element)
if (bindingResult.isSync) {
this.bindingCompletion = bindingResult
Expand Down
2 changes: 1 addition & 1 deletion packages/bind/src/LegacyBindingHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class LegacyBindingHandler extends BindingHandler {
]
}

get controlsDescendants(): any {
get controlsDescendants(): boolean {
const objectToTest = this.initReturn || this.handler || {}
return objectToTest.controlsDescendantBindings
}
Expand Down
6 changes: 3 additions & 3 deletions packages/bind/src/applyBindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ interface BindingError {
during: string,
errorCaptured: any,
bindings?: any,
allBindings?: any,
allBindings?: AllBindings,
bindingKey?: string,
bindingContext: any,
bindingContext: BindingContext,
element: HTMLElement,
valueAccessor?: Function,
message?: string,
Expand Down Expand Up @@ -263,7 +263,7 @@ function applyBindingsToNodeInternal (node: HTMLElement, sourceBindings: any, bi
} : (bindingKey) => bindings[bindingKey]

// Use of allBindings as a function is maintained for backwards compatibility, but its use is deprecated
const allBindings = function () {
const allBindings: AllBindings = function () {
return objectMap(bindingsUpdater ? bindingsUpdater() : bindings, evaluateValueAccessor)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/binding.component/src/componentBinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import registry from '@tko/utils.component'
var componentLoadingOperationUniqueId = 0

export default class ComponentBinding extends DescendantBindingHandler {
childBindingContext: any;
childBindingContext: BindingContext;
currentLoadingOperationId: number | null;
currentViewModel: any;
latestComponentName: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/binding.core/src/attr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@tko/observable'

export var attr = {
update: function (element, valueAccessor, allBindings) {
update: function (element, valueAccessor, allBindings: AllBindings) { // allBindings not used!
var value = unwrap(valueAccessor()) || {}
objectForEach(value, function (attrName, attrValue) {
attrValue = unwrap(attrValue)
Expand Down
2 changes: 1 addition & 1 deletion packages/binding.core/src/checked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {

export var checked = {
after: ['value', 'attr'],
init: function (element, valueAccessor, allBindings) {
init: function (element, valueAccessor, allBindings: AllBindings) {
var checkedValue = pureComputed(function () {
// Treat "value" like "checkedValue" when it is included with "checked" binding
if (allBindings.has('checkedValue')) {
Expand Down
4 changes: 2 additions & 2 deletions packages/binding.core/src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
// e.g. click:handler instead of the usual full-length event:{click:handler}
export function makeEventHandlerShortcut (eventName) {
return {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
init: function (element, valueAccessor, allBindings: AllBindings, viewModel, bindingContext: BindingContext) {
var newValueAccessor = function () {
var result = {}
result[eventName] = valueAccessor()
Expand All @@ -28,7 +28,7 @@ function makeDescriptor (handlerOrObject) {
}

export const eventHandler = {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext: BindingContext) {
var eventsToHandle = valueAccessor() || {}
objectForEach(eventsToHandle, function (eventName, descriptor) {
const {passive, capture, once, debounce, throttle} = makeDescriptor(descriptor)
Expand Down
2 changes: 1 addition & 1 deletion packages/binding.core/src/let.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from '@tko/bind'

export default {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext: BindingContext) { // allBindings and viewModel not used!
// Make a modified binding context, with extra properties, and apply it to descendant elements
var innerContext = bindingContext['extend'](valueAccessor)
applyBindingsToDescendants(innerContext, element)
Expand Down
2 changes: 1 addition & 1 deletion packages/binding.core/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export var options = {
// Ensures that the binding processor doesn't try to bind the options
return { 'controlsDescendantBindings': true }
},
update: function (element, valueAccessor, allBindings) {
update: function (element, valueAccessor, allBindings: AllBindings) {
function selectedOptions () {
return arrayFilter(element.options, function (node) { return node.selected })
}
Expand Down
2 changes: 1 addition & 1 deletion packages/binding.core/src/selectedOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
export var selectedOptions = {
after: ['options', 'foreach'],

init: function (element, valueAccessor, allBindings) {
init: function (element, valueAccessor, allBindings: AllBindings) { // allBindings not in use
registerEventHandler(element, 'change', function () {
var value = valueAccessor(), valueToWrite = new Array()
arrayForEach(element.getElementsByTagName('option'), function (node) {
Expand Down
2 changes: 1 addition & 1 deletion packages/binding.core/src/submit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from '@tko/utils'

export var submit = {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext: BindingContext) { // allBindings and viewModel not in use
if (typeof valueAccessor() !== 'function') { throw new Error('The value for a submit binding must be a function') }
registerEventHandler(element, 'submit', function (event) {
var handlerReturnValue
Expand Down
2 changes: 1 addition & 1 deletion packages/binding.core/src/using.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
} from '@tko/bind'

export var using = {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
init: function (element, valueAccessor, allBindings: AllBindings, viewModel, bindingContext: BindingContext) { // allBindings and viewModel aren't actually used here
var innerContext = bindingContext.createChildContext(valueAccessor)
applyBindingsToDescendants(innerContext, element)
return { controlsDescendantBindings: true }
Expand Down
2 changes: 1 addition & 1 deletion packages/binding.foreach/src/foreach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class ForEachBinding extends AsyncBindingHandler {
// computed
// {data: array, name: string, as: string}
afterAdd;
allBindings;
allBindings: AllBindings;
static animateFrame;
as;
beforeRemove;
Expand Down
2 changes: 1 addition & 1 deletion packages/binding.template/helpers/dummyTemplateEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function dummyTemplateEngine(templates?) {
return new anonymousTemplate(template); // Anonymous template
};

this.renderTemplateSource = function (templateSource, bindingContext, rt_options, templateDocument) {
this.renderTemplateSource = function (templateSource, bindingContext: BindingContext, rt_options, templateDocument) {
var data = bindingContext['$data'];
if (data && typeof data.get_value === 'function') {
// For cases when data is an Identifier/Expression.
Expand Down
2 changes: 1 addition & 1 deletion packages/binding.template/src/nativeTemplateEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function nativeTemplateEngine () {

nativeTemplateEngine.prototype = new templateEngine()
nativeTemplateEngine.prototype.constructor = nativeTemplateEngine
nativeTemplateEngine.prototype.renderTemplateSource = function (templateSource, bindingContext, options, templateDocument) {
nativeTemplateEngine.prototype.renderTemplateSource = function (templateSource, bindingContext: BindingContext, options, templateDocument) {
let version: number;
if (ieVersion instanceof Array) {
version = parseInt(ieVersion[1], 10);
Expand Down
4 changes: 2 additions & 2 deletions packages/binding.template/src/templateEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
export function templateEngine () { };

extend(templateEngine.prototype, {
renderTemplateSource: function (templateSource, bindingContext, options, templateDocument) {
renderTemplateSource: function (templateSource, bindingContext: BindingContext, options, templateDocument) { // templateSource, bindingContext, templateDocument not in use
options.onError('Override renderTemplateSource')
},

Expand All @@ -57,7 +57,7 @@ extend(templateEngine.prototype, {
} else { options.onError(new Error('Unknown template type: ' + template)) }
},

renderTemplate: function (template, bindingContext, options, templateDocument) {
renderTemplate: function (template, bindingContext: BindingContext, options, templateDocument) {
var templateSource = this['makeTemplateSource'](template, templateDocument)
return this.renderTemplateSource(templateSource, bindingContext, options, templateDocument)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/binding.template/src/templating.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function invokeForEachNodeInContinuousRange (firstNode, lastNode, action) {
}
}

function activateBindingsOnContinuousNodeArray (continuousNodeArray, bindingContext, afterBindingCallback) {
function activateBindingsOnContinuousNodeArray (continuousNodeArray, bindingContext: BindingContext, afterBindingCallback) {
// To be used on any nodes that have been rendered by a template and have been inserted into some parent element
// Walks through continuousNodeArray (which *must* be continuous, i.e., an uninterrupted sequence of sibling nodes, because
// the algorithm for walking them relies on this), and for each top-level item in the virtual-element sense,
Expand Down
Loading

0 comments on commit 1695010

Please sign in to comment.